package com.rpframework.module.common.bottom.controller;

import com.rpframework.core.json.JsonResp;
import com.rpframework.core.json.ParameterException;
import com.rpframework.module.common.bottom.entity.Role;
import com.rpframework.module.common.bottom.service.RoleService;
import com.rpframework.module.common.url.RequestDescription;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@RequestMapping
@RequestDescription("角色")
@ResponseBody
public class RoleController {
    private final RoleService roleService;

    @Autowired
    public RoleController(RoleService roleService) {
        this.roleService = roleService;
    }

    @RequestMapping
    @RequestDescription("查询全部")
    public JsonResp list() {
        return roleService.selectAllJson();
    }

    @RequestMapping
    @RequestDescription("模糊查询")
    public JsonResp selectsVague(Role entity) {
        return roleService.fuzzySearchJson(entity);
    }

    @RequestMapping
    @RequestDescription("主键集合查询")
    public JsonResp selectsIds(Long[] ids) {
        return roleService.selectsByIdsJson(ids);
    }

    @RequestMapping
    @RequestDescription("添加或修改")
    public JsonResp addOrUpdate(Role entity) {
        if (entity == null || StringUtils.isBlank(entity.getName())) {
            throw new ParameterException("角色名不能为空");
        }
        return roleService.addOrUpdateJson(entity);
    }

    @RequestMapping
    @RequestDescription("批量删除")
    public JsonResp deleteIds(Long[] ids) {
        return roleService.deleteIdsJson(ids);
    }
}
